package com.shujia.spark.core

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD

object Demo12Action {
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf()
    conf.setMaster("local")
    conf.setAppName("Demo11Partitioner")

    val sc = new SparkContext(conf)

    val studentsRDD: RDD[String] = sc.textFile("data/students.txt")

    //1、count
    val count: Long = studentsRDD.count()

    println(count)

    //2、collect:将数据拉取到内存中
    val studentARR: Array[String] = studentsRDD.collect()
    println(studentARR.toList)

    //3、reduce:全局聚合
    val num: Int = studentsRDD
      .map(s => 1)
      .reduce(_ + _)

    println(num)

    //4、foreach：循环遍历
    studentsRDD.foreach(println)

    //5、sum：全局求和，只能是数值
    val sum: Double = studentsRDD
      .map(s => 1)
      .sum()

    println(sum)

    //take:取前面的数据
    val top10: Array[String] = studentsRDD.take(10)
    println(top10.toList)

    //saveAsTextFile:保存为文本文件
    studentsRDD.saveAsTextFile("data/students")

    studentsRDD.saveAsObjectFile("data/students_obj")

    while (true) {}
  }
}
